<?php

Route::get('user/(:num)', 'user@profile');
Route::controller('user');

Route::filter('user_profile', function() {
    if (!URI::is('user(|/index|/login|/logout|/profile/*)')) {
        return Redirect::to_action('user@profile', 
                                   array('userid' => URI::segment(2)));
    }
});

Route::get('/', array('before' => 'auth', 'do' => function(){
    return Redirect::to_action('mail.domain@index');
}));

Route::get('domain/view/(:num)', array('before' => 'auth', 'do' => function(){
    //
}));

Route::get('mail/alias', array('before' => 'auth', 
                               'uses' => 'mail.alias@index'));

Route::get('mail/domain', array('before' => 'auth', 
                                'uses' => 'mail.domain@index'));

Route::get('mail', array('before' => 'auth', 'do' => function(){
    return Redirect::to_action('mail.domain@index');
}));

Route::get('domain/add', array('before' => 'auth', 'do' => function(){
    //
}));

Route::post('domain/add', array('before' => 'auth', 'do' => function(){
    //
}));

Route::get('domain/delete/(:num)', array('before' => 'auth', 'do' => function(){
    //
}));

Route::post('domain/delete', array('before' => 'auth', 'do' => function(){
    //
}));

Route::get('auth/login', 'jijaf.auth@login');
Route::post('auth/login', array('before' => 'csrf',
                                'uses'   => 'jijaf.auth@handle_login'));

Route::get('auth/profile', array('before' => 'auth',
                                 'uses'   => 'jijaf.auth@profile'));
Route::post('auth/profile', array('before' => 'auth|csrf',
                                  'uses'   => 'jijaf.auth@handle_profile'));

Route::get('auth/logout', 'jijaf.auth@logout');

/*
|--------------------------------------------------------------------------
| Application 404 & 500 Error Handlers
|--------------------------------------------------------------------------
|
| To centralize and simplify 404 handling, Laravel uses an awesome event
| system to retrieve the response. Feel free to modify this function to
| your tastes and the needs of your application.
|
| Similarly, we use an event to handle the display of 500 level errors
| within the application. These errors are fired when there is an
| uncaught exception thrown in the application.
|
*/

Event::listen('404', function()
{
	return Response::error('404');
});

Event::listen('500', function()
{
	return Response::error('500');
});

/*
|--------------------------------------------------------------------------
| Route Filters
|--------------------------------------------------------------------------
|
| Filters provide a convenient method for attaching functionality to your
| routes. The built-in before and after filters are called before and
| after every request to your application, and you may even create
| other filters that can be attached to individual routes.
|
| Let's walk through an example...
|
| First, define a filter:
|
|		Route::filter('filter', function()
|		{
|			return 'Filtered!';
|		});
|
| Next, attach the filter to a route:
|
|		Router::register('GET /', array('before' => 'filter', function()
|		{
|			return 'Hello World!';
|		}));
|
*/

Route::filter('before', function()
{
	// Do stuff before every request to your application...
});

Route::filter('after', function($response)
{
	// Do stuff after every request to your application...
});

Route::filter('csrf', function()
{
	if (Request::forged()) return Response::error('500');
});

Route::filter('auth', function()
{
    if (Auth::guest()) {
        // Save the attempted URL
        Session::put('pre_login_url', URL::current());

        return Redirect::to_action('jijaf.auth@login');
    }
});
